package com.crawler.search.index.suffix;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

/**
 * 根据类型，返回不同的索引类名
 * @author zyh
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VersionSuffixer extends Suffixer {

	public String field;

	public static String version = "_v1";

	@Override
	public String getAliasIndex(String index) {
		return index.replaceAll("_v[0-9]*$", "");
	}

	@Override
	public String createAliasName(JSONObject json) {
		return getVersionSuffix(json);
	}

	/**
	* 
	* 根据设置索引别名后缀
	* @return {@link String}
	**/
	public  String getVersionSuffix(JSONObject json){
		String suffix = version;
		if(!org.springframework.util.StringUtils.isEmpty(field)){
			if(json.containsKey(field)){
				String suff = json.getString(field);
				if(!StringUtils.isEmpty(suff)){
					suffix = "_"+suff;
				}
			}
		}
		return suffix;
	}
}
